﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Transport
{
    public class LoTrinhControl
    {
        transportEntities db;

        public LoTrinhControl()
        {
            db = new transportEntities();
        }

        public lo_trinh findByID(string Id)
        {
            try
            {
                return db.lo_trinh.SingleOrDefault(t => t.ma_lo_trinh == Id);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public lo_trinh findByName(string name)
        {
            try
            {
                return db.lo_trinh.SingleOrDefault(t => t.ten_lo_trinh == name);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string getRegionID(string ID)
        {
            try
            {
                lo_trinh lt = db.lo_trinh.SingleOrDefault(t => t.ma_lo_trinh == ID);
                if (lt != null)
                    return lt.ma_mien;
                else
                    return "";
            }
            catch
            {
                return "";
            }
        }

        public bool insert(lo_trinh value)
        {
            try
            {
                db.lo_trinh.AddObject(value);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool update(lo_trinh value)
        {
            try
            {
                lo_trinh st = db.lo_trinh.SingleOrDefault(t => t.ma_lo_trinh == value.ma_lo_trinh);
                st = value;
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool delete(string Id)
        {
            try
            {
                lo_trinh st = db.lo_trinh.SingleOrDefault(t => t.ma_lo_trinh == Id);
                db.lo_trinh.DeleteObject(st);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}